﻿#include "DialogAddLocalRange.h"
#include "ui_DialogAddLocalRange.h"

#include "AnimationSettingData.h"
#include "DataChosenListDialog.h"
#include <QIntValidator>
#include "IdaWidgetHelper.h"
#include <QListView>
#include <QDebug>
#include "BaseModule/IBaseSignal.h"

namespace pst
{
    DialogAddLocalRange::DialogAddLocalRange(QWidget* parent)
        : IdaDialogFrame(parent), ui(new Ui::DialogAddLocalRange())
    {
        ui->setupUi(this);

        setWindowTitle(QStringLiteral("局部范围"));
        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_confirm); // 将确定设置为主按钮

        ui->pushButton_Curve->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_Curve->setStyleSheet("QPushButton { text-align: center; }");
        ui->doubleSpinBox_End->setRange(-1000000000, 1000000000);
        ui->doubleSpinBox_End->setDecimals(9);
        ui->doubleSpinBox_Begin->setRange(-1000000000, 1000000000);
        ui->doubleSpinBox_Begin->setDecimals(9);
    }

    DialogAddLocalRange::~DialogAddLocalRange()
    {
        delete ui;
    }

    void DialogAddLocalRange::setIsEdit(bool f)
    {
        m_edit = f;
    }

    void DialogAddLocalRange::setInit(QStringList &params)
    {
        if (params[0] != "11")
        {
            return;
        }
        ui->doubleSpinBox_Begin->setValue(params[1].toDouble());
        ui->doubleSpinBox_End->setValue(params[2].toDouble());
        slot_sentChosenDataIds(createSelectedIndexes(params.mid(3)));
    }

    void DialogAddLocalRange::setCurveNameList(const QList<QString>& curveNameList)
    {
        m_curveNameList = curveNameList;
        m_model.setStringList(m_curveNameList);
    }

    void DialogAddLocalRange::on_pushButton_confirm_clicked()
    {
        QList<QString> selectNames;
        for (auto index : m_selectedIndexs)
        {
            selectNames.append(m_curveNameList.at(index));
        }
        bool execFlag = true;
        emit signal_sendLocalScopeData(ui->doubleSpinBox_Begin->value(), ui->doubleSpinBox_End->value(), selectNames, execFlag, m_edit);
        if (execFlag)
        {
            QString _pyCode = QString("PostProcessing.lineChartCenterWidgetDialogAddLocalRange(%1,%2,'%3',%4,%5)")
                                  .arg(ui->doubleSpinBox_Begin->value())
                                  .arg(ui->doubleSpinBox_End->value())
                                  .arg(selectNames.join(',').remove(QRegExp("[\r\n]")))
                                  .arg(execFlag)
                                  .arg(m_edit);
            emit IBaseSignalInstance->signal_execPYCode(QStringList{_pyCode}, true, false);

            IdaDialogFrame::accept();
        }
        else
        {
            IdaDialogFrame::exec();
        }
        
    }

    void DialogAddLocalRange::on_pushButton_cancel_clicked()
    {
        this->close();
    }

    void DialogAddLocalRange::slot_sentChosenDataIds(const QModelIndexList& chosenID)
    {
        m_selectedIndexs.clear();
        auto count = chosenID.count();
        QString strText{};
        for (int i = 0; i < count; ++i)
        {
            auto chosenData = chosenID.at(i);
            if (chosenData.isValid())
            {
                auto str = chosenData.data().toString();
                strText += str + "\n";

                auto index = chosenID.at(i).row();
                m_selectedIndexs.append(index);
            }
        }
        if (!m_selectedIndexs.isEmpty())
            IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_Curve, true, Qt::blue);
        else
            IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_Curve, false, Qt::white);
    }

    void DialogAddLocalRange::on_pushButton_Curve_clicked()
    {
        m_dataChosenListDialog = new DataChosenListDialog();
        m_dataChosenListDialog->setFixedSize(300, 280);
        // m_dataChosenListDialog->moveToCenter();
        m_dataChosenListDialog->setData(m_curveNameList);
        if (m_edit)
        {
            m_dataChosenListDialog->setMultipleSelected(false);
        }
        else
        {
            m_dataChosenListDialog->setMultipleSelected(true);
        }
        m_dataChosenListDialog->updateUI();
        m_dataChosenListDialog->showWindow();
        m_dataChosenListDialog->setSelctedIndexs(m_selectedIndexs); // 默认选中第一个

        connect(m_dataChosenListDialog,
                &DataChosenListDialog::signal_sentChosenDataIds,
                this, &DialogAddLocalRange::slot_sentChosenDataIds);
    }

    // 生成模拟ListView选中项的QModelIndexList
    QModelIndexList DialogAddLocalRange::createSelectedIndexes(const QStringList & params) {
        QModelIndexList selectedIndexes;

        foreach(const QString & param, params) {
            int row = m_curveNameList.indexOf(param);
            if (row != -1) {
                // 使用成员模型m_model创建索引，确保索引与模型生命周期一致
                QModelIndex index = m_model.index(row, 0);
                if (index.isValid()) {
                    selectedIndexes.append(index);
                }
            }
        }
        return selectedIndexes;
    }
} // namespace pst
